﻿Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class admin_manageforums_home
        Inherits ActiveAdminBase
        Private arrowUp As String = String.Empty
        Private arrowDown As String = String.Empty
        Private edit As String = String.Empty

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            arrowUp = "<img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/arrow_up.png") & """ alt=""" & GetSharedResource("[RESX:MoveUp]") & """ />"
            arrowDown = "<img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/arrow_down.png") & """ alt=""" & GetSharedResource("[RESX:MoveDown]") & """ />"
            edit = "<img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/forum_edit.png") & """ alt=""" & GetSharedResource("[RESX:Edit]") & """ />"
            If Not cbGrid.IsCallback Then
                BindForums()
            End If

        End Sub
        Private Sub BindForums()
            Dim dr As IDataReader = DataProvider.Instance.Forums_List(PortalId, ModuleId, -1, -1, False)
            Dim dt As New DataTable("Forums")
            Dim row As DataRow = Nothing
            dt.Load(dr)
            Dim totalGroups As Integer = GetGroupCount(dt)
            Dim totalGroupForum As Integer = 0
            Dim tmpGroup As String = String.Empty
            Dim i As Integer = 0
            Dim groupCount As Integer = 0
            Dim forumCount As Integer = 0
            Dim sb As New Text.StringBuilder()
            sb.Append("<table width=""95%"" border=""0"" cellpadding=""0"" cellspacing=""0"">")
            For Each row In dt.Rows
                If tmpGroup <> row("ForumGroupId").ToString Then
                    If groupCount < Globals.GroupCount Then
                        Dim sGroupName As String = row("GroupName").ToString
                        If groupCount > 0 Then
                            sb.Append("<tr><td colspan=""8"" width=""100%"">" & RenderSpacer(5, 100) & "</td></tr>")
                        End If
                        sb.Append("<tr class=""afgroupback""><td class=""afgroupback_left"">" & RenderSpacer(1, 4) & "</td><td colspan=""3"" width=""100%"" onmouseover=""this.className='agrowedit'"" onmouseout=""this.className=''"" onclick=""LoadView('manageforums_forumeditor','" & row("ForumGroupId").ToString() & "|G');"">")
                        sb.Append(sGroupName)
                        sb.Append("</td><td><div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""LoadView('manageforums_forumeditor','" & row("ForumGroupId").ToString() & "|G');"">" & edit & "</div></td><td>")
                        If groupCount > 0 Then
                            sb.Append("<div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""groupMove(" & row("ForumGroupId").ToString & ",-1);"">" & arrowUp & "</div>")
                        End If
                        groupCount += 1
                        sb.Append("</td><td>")
                        If groupCount < totalGroups Then
                            sb.Append("<div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""groupMove(" & row("ForumGroupId").ToString & ",1);"">" & arrowDown & "</div>")
                        End If
                        sb.Append("</td><td class=""afgroupback_right"">" & RenderSpacer(1, 4) & "</td></tr>")
                        forumCount = 0
                        totalGroupForum = GetGroupForumCount(dt, CInt(row("ForumGroupId")))
                        tmpGroup = row("ForumGroupId").ToString
                    End If

                End If
                i += 1
                If CInt(row("ParentForumId")) = 0 Then
                    If forumCount < Globals.ForumCount Then
                        Dim sForumName As String = row("ForumName").ToString
                        sb.Append("<tr class=""afforumback""><td class=""afforumback_left"">" & RenderSpacer(1, 4) & "</td><td style=""width:15px;"" width=""15"">" & RenderSpacer(5, 15) & "</td><td colspan=""2"" width=""100%"" onmouseover=""this.className='afrowedit'"" onmouseout=""this.className=''"" onclick=""LoadView('manageforums_forumeditor','" & row("ForumId").ToString() & "|F');"">")
                        sb.Append(sForumName)
                        sb.Append("</td><td><div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""LoadView('manageforums_forumeditor','" & row("ForumId").ToString() & "|F');"">" & edit & "</div></td><td>")
                        If forumCount > 0 Then
                            sb.Append("<div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""forumMove(" & row("ForumId").ToString & ",-1);"">" & arrowUp & "</div>")
                        End If
                        forumCount += 1
                        sb.Append("</td><td>")
                        If forumCount < totalGroupForum Then
                            sb.Append("<div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""forumMove(" & row("ForumId").ToString & ",1);"">" & arrowDown & "</div>")
                        End If
                        sb.Append("</td><td class=""afforumback_right"">" & RenderSpacer(1, 4) & "</td></tr>")
#If SKU <> "LITE" Then
                        If HasSubForums(CInt(row("ForumId")), dt) > 0 Then
                            sb.Append(AddSubForums(dt, row))
                        End If
#End If

                    End If


                End If

            Next

            If Not dr.IsClosed Then
                dr.Close()
            End If
            sb.Append("<tr><td></td><td></td><td></td><td width=""100%""></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td width=""100%"" colspan=""2""></td><td></td><td></td><td></td><td></td></tr></table>")
            litForums.Text = sb.ToString

        End Sub
        Private Function RenderSpacer(ByVal Height As Integer, ByVal Width As Integer) As String
            Return "<img src=""" & Page.ResolveUrl("~/DesktopModules/ActiveForums/images/spacer.gif") & """ height=""" & Height & """ width=""" & Width & """ alt=""-"" />"
        End Function
        Private Function GetGroupCount(ByVal dt As DataTable) As Integer
            Dim i As Integer = 0
            Dim row As DataRow = Nothing
            Dim tmpGroup As String = String.Empty
            For Each row In dt.Rows
                If tmpGroup <> row("ForumGroupId").ToString Then
                    i += 1
                    tmpGroup = row("ForumGroupId").ToString
                End If
            Next
            Return i
        End Function
        Private Function GetGroupForumCount(ByVal dt As DataTable, ByVal GroupId As Integer) As Integer
            Dim i As Integer = 0
            Dim row As DataRow = Nothing
            Dim tmpGroup As String = String.Empty
            For Each row In dt.Rows
                If CInt(row("ForumGroupId")) = GroupId And CInt(row("ParentForumId")) = 0 Then
                    i += 1
                End If
            Next
            Return i

        End Function
        Private Function AddSubForums(ByVal dt As DataTable, ByVal dr As DataRow) As String
            Dim row As DataRow = Nothing
            Dim sb As New Text.StringBuilder()
            Dim totalSubCount As Integer = HasSubForums(CInt(dr("ForumId")), dt)
            Dim subCount As Integer = 0
            For Each row In dt.Rows
                If CInt(dr("ForumId")) = CInt(row("ParentForumId")) Then
                    Dim sForumName As String = row("ForumName").ToString
                    sb.Append("<tr class=""afforumback""><td class=""afforumback_left"">" & RenderSpacer(1, 4) & "</td><td style=""width:15px;"">" & RenderSpacer(5, 15) & "</td><td style=""width:15px;"">" & RenderSpacer(5, 15) & "</td><td width=""100%"" onmouseover=""this.className='afrowedit'"" onmouseout=""this.className=''"" onclick=""loadEditorView('F'," & row("ForumId").ToString & ");"">")
                    sb.Append(sForumName)
                    sb.Append("</td><td><div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""loadEditorView('F'," & row("ForumId").ToString & ");"">" & edit & "</div></td><td>")
                    If subCount > 0 Then
                        sb.Append("<div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""forumMove(" & row("ForumId").ToString & ",-1);"">" & arrowUp & "</div>")
                    End If
                    subCount += 1
                    sb.Append("</td><td>")
                    If subCount < totalSubCount Then
                        sb.Append("<div class=""af16icon"" onmouseover=""this.className='af16icon_over';"" onmouseout=""this.className='af16icon';"" onclick=""forumMove(" & row("ForumId").ToString & ",1);"">" & arrowDown & "</div>")
                    End If
                    sb.Append("</td><td class=""afforumback_right"">" & RenderSpacer(1, 4) & "</td></tr>")
                End If
            Next
            Return sb.ToString
        End Function
        Private Function HasSubForums(ByVal ForumId As Integer, ByVal dt As DataTable) As Integer
            Dim row As DataRow = Nothing
            Dim subCount As Integer = 0
            For Each row In dt.Rows
                If CInt(row("ParentForumId")) = ForumId Then
                    subCount += 1
                End If
            Next
            Return subCount
        End Function

        Private Sub cbGrid_Callback(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.CallBackEventArgs) Handles cbGrid.Callback
            Dim objectId As Integer = CInt(e.Parameters(1))
            Dim dir As Integer = CInt(e.Parameters(2))
            Select Case e.Parameters(0).ToString.ToLower
                Case "g"
                    DataProvider.Instance.Groups_Move(ModuleId, objectId, dir)
                Case "f"
                    DataProvider.Instance.Forums_Move(ModuleId, objectId, dir)
            End Select
            BindForums()
            litForums.RenderControl(e.Output)
        End Sub
    End Class
End Namespace